package com.atguigu.juc.locks;

import java.util.concurrent.TimeUnit;

/**
 * AB两个线程标准访问，请问先打印邮件还是短信？
 * @author yfstart
 * @create 2022-06-27 20:04
 */
public class Lock8Demo1 {
  public static void main(String[] args) {
      Phone1 phone = new Phone1(); // this,当前实例对象
      new Thread(phone::sendEmail,"A").start();

      // 保证线程A在线程B之前执行
      try { TimeUnit.MILLISECONDS.sleep(500); } catch (InterruptedException e) { e.printStackTrace();}

      new Thread(phone::sendSMS,"B").start();

  }
}

/** 资源类 Phone.java---Phone.class(字节码文件小class)---加载进入JVM形成模板大Class Phone.Class */
class Phone1 {
  public void sendEmail() {
    synchronized (this) {
      System.out.println("-----Email");
    }
  }

  public synchronized void sendSMS() {
    synchronized (this) {
      System.out.println("-----SMS");
    }
  }
}
